#!/bin/bash

# 主应用启动脚本
# 远程服务器信息
SERVER="root@182.160.6.140"
DEPLOY_PATH="/var/www/binance-analysis"
APP_PORT=3000

echo "开始启动主应用..."

# 先杀死可能占用3000端口的进程
ssh $SERVER "lsof -t -i:$APP_PORT | xargs -r kill -9"
sleep 2

# 停止旧的应用
echo "停止旧的主应用..."
ssh $SERVER "pm2 stop binance-analysis || true"
ssh $SERVER "pm2 delete binance-analysis || true"
sleep 1

# 再次检查端口是否被占用
echo "检查$APP_PORT端口占用情况..."
ssh $SERVER "lsof -t -i:$APP_PORT | xargs -r kill -9"
sleep 1

# 启动主应用
echo "启动主应用..."
ssh $SERVER "cd $DEPLOY_PATH && PORT=$APP_PORT pm2 start src/server.js --name binance-analysis"

# 保存PM2配置
ssh $SERVER "pm2 save"

# 显示运行状态
echo "显示服务运行状态..."
ssh $SERVER "pm2 list"

echo "主应用启动操作已完成！" 